// pages/orderdetails/orderdetails.js

const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const wxpay = require('../../services/pay.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openSelectTime: false,
    selectTimeDone: false,
    orderid: "",
    communityid: "",

    address: "",
    area: "",
    codeid: "",
    community: "",
    mobile: "",
    name: "",
    reservetime: "",
    serviceinfo: [],
    sex: "",
    status: "",
    street: "",
    totalfee: "0",
    tradeno: "",
    usertype: "",
    ispay: -1,
    statusnum: 0,
    reservetimes: '请选择预约时间',
    tiems: '请选择预约时间段',
    reservetimedata: [],
    timeid: 0,
    stais: 1,
    utype: 0,
    stades: ''

  },
  /**
   * 反馈按钮点击事件
   */
  opinionReback: function() {
    let that = this;
    let statuscode= that.data.statuscode;
    if (statuscode==1){
      wx.navigateTo({
        url: '../reback/reback?orderId=' + that.data.orderid
      })
    }
    else{
      util.showErrorToast("订单未完成");
    }
   
  },
  //重新预约
  Rescheduling: function() {
    let that = this;
    console.log(that.data.statusnum);
    if (that.data.ispay == 1 && that.data.statusnum != 0) {
      that.setData({
        openSelectTime: true
      })

    } else {
      util.showErrorToast("不可以预约");
    }

  },
  // 确定重新预约
  doneSelectTime() {
    let that = this;
    if (this.data.selectTimeDone === false) {
      return false;
    }
    util.request(api.Order, {
      'cont': 3,
      'orderid': that.data.orderid,
      'time': that.data.reservetimes,
      'reservetimeid': that.data.timeid
    }, 'POST').then(function(res) {
      console.log(res);
      if (res.code == 0) {

        // wx.showToast({
        //   title: '预约成功',
        //   icon: 'success',
        //   duration: 2000,
        // });
        let data = '';
        for (let i = 0; i < res.data.length; i++) {
          if (res.data[i].last > 0)
            data += res.data[i].name + '\n';
        }
        wx.showModal({
          title: '本次预约服务',
          content: data,
          success: function(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })

        that.getorderdetails();
        that.setData({
          openSelectTime: false,
          selectTimeDone: false,
          reservetimes: '请选择预约时间',
          tiems: '请选择预约时间段',
        })

      } else if (res.code == 10) {
        util.showErrorToast("你已经没有剩余次数");
      }
    });

  },
  // 取消重新预约时间的选择
  cancelSelectTime: function() {
    let that = this;
    that.setData({
      openSelectTime: false
    })
  },
  toPayTap: function() {
    let that = this;
    let userids = wx.getStorageSync('userids');
    wxpay.wxpay(that.data.totalfee, that.data.tradeno, '', userids);
    that.getorderdetails();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options);
    let that = this;
    this.setData({
      orderid: options.orderId
    });
    that.getorderdetails();
  },

  getorderdetails: function() {
    let that = this;
    util.request(api.Appoint, {
      'cont': 2,
      'appointid': this.data.orderid
    }, 'POST').then(function(res) {
      console.log(res);
      if (res.code == 0) {

        that.setData({

          address: res.data.address,
          area: res.data.area,
          codeid: res.data.codeid,
          community: res.data.community,
          mobile: res.data.mobile,
          name: res.data.username,
          reservetime: res.data.reservetime,
          statuscode: res.data.statuscode,
          sex: res.data.sex,
          status: res.data.status,
          street: res.data.street,

          stais: res.data.stais,
          usertype: res.data.usertype,



        })

        if (res.data.stais != 0) {
          that.setData({
            stades: res.data.stades
          })
        }
      } else if (res.code == 400) {

      } else {
        util.showErrorToast("数据获取失败" + res.code);
      }
    });

  },
  //  预约时间
  bindDateChange: function(e) {
    let that = this;




    this.setData({
      tiems: '请选择预约时间段',
      reservetimes: e.detail.value
    })
    that.getReserveTime();
  },
  // 时间
  selReserveTime: function(e) {
    var index = e.detail.value;
    let that = this;
    let reserve = that.data.userReserveInfo;

    that.setData({
      selectTimeDone: true,
      tiems: that.data.reservetimedata[index].info,

      timeid: that.data.reservetimedata[index].id,
    })
  },
  getReserveTime() {
    let that = this;
    if (that.data.reservetimes == '请选择预约时间') {
      return;
    }
    util.request(api.Reserve, {
      'cont': '4',
      'time': that.data.reservetimes,
      'communityid': that.data.communityid
    }, 'POST').then(function(res) {
      console.log(res);
      if (res.code == 0) {
        if (res.data.length <= 0)
          util.showErrorToast("当天预约已满");
        that.setData({
          reservetimedata: res.data,
        })
      } else if (res.code == 400) {

      } else if (res.code == 10) {
        util.showErrorToast("当天预约已满");
      } else {
        util.showErrorToast("数据获取失败" + res.code);
      }
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let usertype = wx.getStorageSync('usertype');
    this.setData({
      utype: usertype
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})